package net.ojbk.timeAxis.service;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import net.ojbk.timeAxis.po.Img;
import net.ojbk.timeAxis.po.Msg;
import net.ojbk.timeAxis.po.Time;
import net.ojbk.timeAxis.tool.DBTool;
import net.ojbk.timeAxis.tool.StringTool;

public class TimeService {
	public int publishTime(String timeId, String axisId, String timeTitle, String timeInfo, Date timeDate, String imgId, String route) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			Time time = new Time();
			time.setAxisId(axisId);
			time.setTimeDate(timeDate);
			time.setTimeId(timeId);
			time.setTimeInfo(timeInfo);
			time.setTimeTitle(timeTitle);
			
			System.out.println(time);
			sqlSession.insert("timeMap.insertOne", time);
			
			Img img = new Img();
			img.setImgId(imgId);
			img.setRoute(route);
			img.setTimeId(timeId);
			System.out.println(img);
			sqlSession.insert("imgMap.insertOne", img);
			return 1;
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	
	public int deleteMsg(String msgId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			sqlSession.delete("msgMap.deleteOne", msgId);
			return 1;
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	
	public int publishMsg(String timeId, String msgName, String msgContent) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			Msg msg = new Msg();
			msg.setMsgContent(msgContent);
			msg.setMsgDate(new Date());
			msg.setMsgName(msgName);
			msg.setTimeId(timeId);
			msg.setMsgId(StringTool.getRandomNumberString());
			sqlSession.delete("msgMap.insertOne", msg);
			return 1;
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	// JSP 数据部分
	
	public Time getTimeInfo(String timeId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			return sqlSession.selectOne("timeMap.selectOne", timeId);
		} catch(Exception e) {
			return null;
		} finally {
			sqlSession.close();
		}
	}
}
